import {
    Ordinary
} from './ordinary-model.js'
const app = getApp()
const ordinary = new Ordinary()
Page({
    /**
     * 页面的初始数据
     **/
    data: {
        userInfo: null,
        allowClick: true
    },
    /*
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.aldstat.sendEvent('升级会员页面');
        if (wx.getStorageSync('userStatus') === 1) {
            wx.switchTab({
                url: '/pages/index/index'
            })
            return
        }
        let that = this;
        //判断用户是否登录
        let userInfo = app.globalData.userInfo;
        if (userInfo) {
            this.setData({
                userInfo: userInfo,
            })
        } else {
            wx.getUserInfo({
                success: function (res) {
                    that.setData({
                        userInfo: res.userInfo
                    })
                }
            })
        }
        this.memberNum();
    },
    //购买会员
    createOrder() {
      app.aldstat.sendEvent('升级会员-开通会员');
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        if (!this.data.allowClick) {
            return
        }
        this.setData({
            allowClick: false
        })
        let inviteMobile = {
            refereeMobile: wx.getStorageSync('invitePhone') || ''
        }
        console.log('inviteMobile', inviteMobile)
        ordinary.rechargeMemberOrder(inviteMobile, res => {
            if (res.status) {
                let data = res.data
                let params = {
                    orderId: data.orderId,
                    wxuid: wx.getStorageSync('wxUid')
                }
                ordinary.doUnifiedOrderForRechargeMember(params, res2 => {
                    wx.hideLoading()
                    let data = res2.data
                    if (res2.status) {
                        console.log('res', res2, res2.data, 'prepay_id=' + data.miniPaySign)
                        that.setData({
                            allowClick: true
                        })
                        if (data.result_code == "SUCCESS" && data.return_code == "SUCCESS") {
                            wx.requestPayment({
                                'timeStamp': '' + data.miniTimeStamp,
                                'nonceStr': '' + data.miniNonceStr,
                                'package': 'prepay_id=' + data.prepay_id,
                                'signType': 'MD5',
                                'paySign': '' + data.miniPaySign,
                                'success': function (result) {
                                    console.log('pay', result)
                                    wx.setStorageSync('userStatus', 1);
                                    // wx.redirectTo({
                                    //     url: '/pages/role/member/member?vipSuccess=ok',
                                    // })
                                    wx.setStorageSync('vipModelShow', 'show');
                                    wx.switchTab({
                                        url: '/pages/index/index',
                                    })
                                },
                                'fail': function (result) {
                                    that.setData({
                                        allowClick: true
                                    })
                                }
                            })
                        }

                    } else {
                        wx.showToast({
                            title: '服务器错误',
                            icon: 'none'
                        })
                    }
                })


            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })
    },
    goIndex() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //第多少位成员
    memberNum() {
        var Num = "";
        for (var i = 0; i < 5; i++) {
            Num += Math.floor(Math.random() * 10);
            if (i == 0 && Num == "0") {
                Num = "1"
            }
        }
        this.setData({
            memberNum: Num
        })
    }

})